$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;
    var focus = true;
    var transportNumber = "";
    var deliverRopeCode = JSON.parse(localStorage.getItem("deliverRopeCode"));
    var carNumber = "";

    //点击返回按钮
    $(".head").on("click", ".goBack", function () {
        $.confirm("确定返回吗？", function () {
            localStorage.removeItem("deliverRopeCode");
            window.location.href = "deliverRope.html";
        }, function () {
            $("#goods_id").focus();
        });
    });

    //根据发货单号显示明细
    loading_show("获取中...");
    $.ajax({
        headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
        type: "post",
        url: url + '/app/deliver/getRopeSaleoutordersByDeliverCode',
        data: {
            deliverCode: deliverRopeCode
        },
        dataType: 'json',
        json: 'callback',
        cache: false,
        timeout: 60000,
        success: function (res) {
            if (res.success) {
                //清空页面中的发货单
                $("#work_list").find("li").remove();
                //隐藏提示符 显示
                loading_hide();
                var data = res.result;
                var data_html = '';
                for (var i = 0; i < data.length; i++) {
                    transportNumber = data[i].carNumber ? data[i].carNumber : "";
                    carNumber = data[i].carNumber ? data[i].carNumber : "";
                    data_html += '<li>' +
                        '<div class="row no-gutter li_content"><div class="col-100">' +
                        '<p class="p_none"><span>发货单id:</span><span class="sp goods_id">' + data[i]._id + '</span></p>' +
                        '<div class="">' +
                        '<p><span>单号:</span><span class="sp goods_deliverRopeCode">' + data[i].number + '</span></p>' +
                        '<p><span>排产单号:</span><span class="sp goods_workplanNumber">' + (data[i].workplanNumber ? data[i].workplanNumber : data[i].relationCompanyWorkplanNumber) + '</span></p>' +
                        '<p><span>名称:</span><span class="sp goods_productName">' + data[i].inventoryName + '</span></p>' +
                        '<p><span>规格:</span><span class="sp goods_specification">' + data[i].inventorySpecification + '</span></p>' +
                        '<p><span>订单类型:</span><span class="sp goods_saleordertype">' + (data[i].saleordertype ? data[i].saleordertype : "") + '</span></p>' +
                        '<p><span>预计重量:</span><span class="sp goods_weight">' + data[i].planQuantity + '</span>T</p>' +
                        '<p><span>已发件数:</span><span class="sp goods_deliverRopeCount">' + (data[i].count ? data[i].count : 0) + '</span>' +
                        '<span>已发重量:</span><span class="sp goods_deliverRopeNetWeight">' + (data[i].quantity ? data[i].quantity : 0) + '</span>T</p>' +
                        '<p><span>未发重量:</span><span class="sp goods_deliverRopeNetWeightW">' + (accSub(data[i].planQuantity, data[i].quantity ? data[i].quantity : 0)) + '</span>T</p>' +
                        '<p><span>仓库:</span><span class="sp goods_houseName">' + data[i].warehouseName + '</span></p>';
                    data_html += '</div></div></div></li>';
                }
                $("#work_list").append(data_html);
                //显示本车本人数据
                //$("div.details span.good_weight").html(result.allData.weights);
                $("div.details span.good_netweight").html(res.allData.net_weights);
            } else {
                loading_hide();
                $alert(res.message);
            }
        },
        error: function (error) {
            loading_hide();
            if (error.status == 0) {
                //loading_hide();
                $alert("请求超时，请重新操作！");
            } else {
                //loading_hide();
                $alert(error.responseText);
            }
        }
    });

    //点击 显示 隐藏
    $(".head .icon-xianshi").on("click", function () {
        $(this).hide();
        $(".head .icon-yincang").show();
        $(".details").show();
        //$alert("显示成功");
        setTimeout(function () {
            $("#goods_id").val("").focus();
        }, 1400);
    });
    $(".head .icon-yincang").on("click", function () {
        $(this).hide();
        $(".head .icon-xianshi").show();
        $(".details").hide();
        //$alert("隐藏成功");
        setTimeout(function () {
            $("#goods_id").val("").focus();
        }, 1400);
    });

    // li 中的 减号 点击 删除 本条
    $(".content").on("click", ".goodsList_ul li .col-10", function () {
        var jianhao = $(this);
        $.confirm('确定删除此货品？', function () {
            jianhao.parent().parent().remove();
            badge_text();
            $alert("删除成功");
            $("#goods_id").val("").focus();
        });
    });

    // 点击 确认发货 信息 传给后台
    $(document).on("click", ".foot .add_success", function () {
        if (typeof(transportNumber) == "string" || transportNumber.length == 0) {
            $("div.div_fh .transportNumber_input").show().val(carNumber).focus();
            $("div.div_fh .transportNumber_select").hide();
            $(".mengceng").show();
            $("div.div_fh").show();
        } else {
            var html = "";
            for (var i = 0; i < transportNumber.length; i++) {
                html += '<option value="' + transportNumber[i] + '">' + transportNumber[i] + '</option>'
            }
            $("div.div_fh .transportNumber_input").hide();
            $("div.div_fh .transportNumber_select").html(html);
            $("div.div_fh .transportNumber_select").show();
            $(".mengceng").show();
            $("div.div_fh").show();
        }
    });
    $("div.div_fh .pt_quxiao").on("click", function () {
        $(".mengceng").hide();
        $("div.div_fh").hide();
    });
    $("div.div_fh .pt_queding").on("click", function () {
        var car = "";
        if (typeof(transportNumber) == "string" || transportNumber.length == 0) {
            car = $("div.div_fh .transportNumber_input").val();
        } else {
            car = $("div.div_fh .transportNumber_select").val();
        }
        var specifications = [];
        for (var i = 0; i < $("#work_list li").length; i++) {
            if ($("#work_list li").eq(i).find('span.goods_deliverRopeNetWeight').text() == '0') {
                specifications.push($("#work_list li").eq(i).find('span.goods_specification').text());
            }
        }
        var confirmHtml = '';
        if (specifications.length > 0) {
            confirmHtml = specifications.join(',') + '规格未发，本车确定发货完成？';
        } else {
            confirmHtml = '本车确定发货完成？'
        }
        $.confirm(confirmHtml, function () {
            if (pass == 0) {
                return
            }
            pass = 0;
            loading_show("发货中...");
            //获取 货品列表中的信息
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "POST",
                url: url + "/app/deliver/saveRopeSaleoutorder",
                data: {
                    car: car,
                    deliverCode: deliverRopeCode
                },
                timeout: 60000,
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        console.log("销售发货成功");
                        localStorage.removeItem("deliverRope");
                        window.location.href = "deliverRope.html";
                    } else {
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    if (error.status == 0) {
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        });
    });

    //点击 某一条 数据  扫描发货
    $("#work_list").on("click", "li", function () {
        var code = $(this).find("span.goods_deliverRopeCode").text();
        var id = $(this).find("span.goods_id").text();
        var workplanNumber = $(this).find("span.goods_workplanNumber").text();
        var houseName = $(this).find("span.goods_houseName").text();
        var specification = $(this).find("span.goods_specification").text();
        var weight = $(this).find("span.goods_weight").text();
        var deliverRopeNetWeight = $(this).find("span.goods_deliverRopeNetWeight").text();
        loading_show("选择中...");
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/deliver/selectRopeSaleoutorder',
            data: {
                _id: id
            },
            dataType: 'json',
            json: 'callback',
            cache: false,
            timeout: 60000,
            success: function (res) {
                if (res.success) {
                    var data = res.result;
                    loading_hide();
                    var deliverRope = {
                        locationInventorys: data,
                        id: id,
                        code: code,
                        houseName: houseName,
                        workplanNumber: workplanNumber,
                        specification: specification,
                        weight: parseInt(accSub(weight, deliverRopeNetWeight))
                    };
                    localStorage.setItem("deliverRope", JSON.stringify(deliverRope));
                    window.location.href = "deliverRope_two.html";
                } else {
                    loading_hide();
                    $.alert(res.message);
                }
            },
            error: function (error) {
                loading_hide();
                if (error.status == 0) {
                    //loading_hide();
                    $alert("请求超时，请重新操作！");
                } else {
                    //loading_hide();
                    $alert(error.responseText);
                }
            }
        });
    });

    // goodsList中 badge 的值 为 列表中 li 的个数
    function badge_text() {
        var badge_len = $(".content .goodsList_ul li").length;
        //console.log(badge_len);
        var allWeight = 0;
        for (var i = 0; i < badge_len; i++) {
            var weight = $(".content .goodsList_ul li").eq(i).find(".row span.goods_cpzl").html();
            allWeight = accAdd(allWeight, weight);
        }
        $(".details span.good_length").html(badge_len);
        $(".details span.good_weight").html(allWeight);
    }

    badge_text();

});